#!/usr/bin/python

import ConfigParser
import os
import re

def get_script_path():
    return os.path.dirname(os.path.abspath(__file__))

def get_config_path():
    config_path = os.path.join(get_script_path(), '../application/configs')
    return os.path.realpath(config_path)

def get_log_path():
    log_path = os.path.join(get_script_path(), '../data/logs')
    return os.path.realpath(log_path)

def get_sql_path():
    sql_path = os.path.join(get_script_path(), '../sql')
    return os.path.realpath(sql_path)

def get_config_file():
    return '/'.join([get_config_path(), 'application.ini'])

def get_redcross_sql_file():
    return '/'.join([get_sql_path(), 'redcross.sql'])

def get_vanilla_sql_file():
    return '/'.join([get_sql_path(), 'vanilla.sql'])

def get_vanilla_path():
    vanilla_path = os.path.join(get_script_path(), '../public/forum')
    return os.path.realpath(vanilla_path)

def get_rkheaderplugin_path():
    plugin_path = os.path.join(get_vanilla_path(), 'plugins/RKHeaderLink')
    return '/'.join([plugin_path, 'class.rkheaderlink.plugin.php'])

def get_vanilla_conf():
    vanilla_config_path = os.path.join(get_vanilla_path(), 'conf')
    return '/'.join([vanilla_config_path, 'config.php'])

def get_navigation_path():
    return '/'.join([get_config_path(), 'navigation.xml'])


config = ConfigParser.RawConfigParser()
config.read(get_config_file())

# dun dun dun

def get_connection_string():
    conn = config.get('production', 'resources.doctrine.connection_string') 
    conn = conn.strip(' "\'')    
    conn = re.findall(r'.*://([^:]+):([^@]+)@([^:/]+):([\d]{0,10})/(.*)', conn, re.UNICODE)
    return conn[0]

def get_url():
    url = config.get('production', 'general.url')
    url = url.strip(' "\'')
    return url

def get_mail_info():
    name = config.get('production', 'mailer.sender_name').strip(' "\'')
    addr = config.get('production', 'mailer.sender').strip(' "\'')
    return (name, addr)

def import_redcross_db():
    c = get_connection_string()
    sql = get_redcross_sql_file()
    print os.system('mysql --user=%s --password=%s --host=%s --port=%s --database=%s < %s' % (c[0], c[1], c[2], c[3], c[4], sql))
    print "redcross db imported"

def import_vanilla_db():
    c = get_connection_string()
    sql = get_vanilla_sql_file()
    print os.system('mysql --user=%s --password=%s --host=%s --port=%s --database=%s < %s' % (c[0], c[1], c[2], c[3], c[4], sql))
    print "vanilla db imported"

def config_vanilla_sql():
    base_url = get_url()
    sql = get_vanilla_sql_file()
    proxy_url = ''.join([base_url, 'proxy-connect'])
    register_url = ''.join([base_url, 'user/create'])
    login_url = ''.join([base_url, 'user/login'])
    logout_url = ''.join([base_url, 'user/logout'])

    os.system("sed -i 's/base_url/%s/g' %s" % (re.escape(base_url), sql))
    os.system("sed -i 's/proxy_url/%s/g' %s" % (re.escape(proxy_url), sql))
    os.system("sed -i 's/register_url/%s/g' %s" % (re.escape(register_url), sql))
    os.system("sed -i 's/login_url/%s/g' %s" % (re.escape(login_url), sql))
    os.system("sed -i 's/logout_url/%s/g' %s" % (re.escape(logout_url), sql))

    print "vanilla sql configured"

def config_navigation():
    base_url = get_url()
    forum_url = ''.join([base_url, 'forum'])
    nav = get_navigation_path()
    os.system("sed -i 's/forum_url/%s/g' %s" % (re.escape(forum_url), nav))

    print "navigation configured"

def config_vanilla():
    c = get_connection_string()
    conf = get_vanilla_conf()
    os.system("sed -i 's/db_user/%s/g' %s" % (re.escape(c[0]), conf))
    os.system("sed -i 's/db_password/%s/g' %s" % (re.escape(c[1]), conf))
    os.system("sed -i 's/db_host/%s/g' %s" % (re.escape(c[2]), conf))
    os.system("sed -i 's/db_port/%s/g' %s" % (re.escape(c[3]), conf))
    os.system("sed -i 's/db_name/%s/g' %s" % (re.escape(c[4]), conf))
    print "vanilla configured"

def config_rkheaderplugin():
    url = get_url()
    plugin = get_rkheaderplugin_path()
    os.system("sed -i 's/shift_management_url/%s/g' %s" % (re.escape(url), plugin))

def config_scripts():
    mail = get_mail_info()
    c = get_connection_string()
    script_path = get_script_path()
    script = '/'.join([script_path, 'unconfirmed_shift_notifications.sh'])
    os.system("sed -i 's/db_user/%s/g' %s" % (re.escape(c[0]), script))
    os.system("sed -i 's/db_pass/%s/g' %s" % (re.escape(c[1]), script))
    os.system("sed -i 's/db_host/%s/g' %s" % (re.escape(c[2]), script))
    os.system("sed -i 's/db_port/%s/g' %s" % (re.escape(c[3]), script))
    os.system("sed -i 's/db_name/%s/g' %s" % (re.escape(c[4]), script))
    os.system("sed -i 's/sender_name/%s/g' %s" % (re.escape(mail[0]), script))
    os.system("sed -i 's/sender_addr/%s/g' %s" % (re.escape(mail[1]), script))
    print "unconfirmed shift notification script configured"

    script = '/'.join([script_path, 'vanilla_sync.py'])
    os.system("sed -i 's/db_user/%s/g' %s" % (re.escape(c[0]), script))
    os.system("sed -i 's/db_pass/%s/g' %s" % (re.escape(c[1]), script))
    os.system("sed -i 's/db_host/%s/g' %s" % (re.escape(c[2]), script))
    os.system("sed -i 's/db_port/%s/g' %s" % (re.escape(c[3]), script))
    os.system("sed -i 's/db_name/%s/g' %s" % (re.escape(c[4]), script))
    print "vanilla sync script configured"

def mkcachedir():
    script_path = get_script_path()
    script = '/'.join([script_path, 'mkcachedir.sh'])
    os.system("sh %s" % script)

def setup_cron():
    script_path = get_script_path()
    log_path = get_log_path()
    print os.system('crontab -l > /tmp/tmpcron')
    os.system("sed -i '/vanilla_sync/ d' /tmp/tmpcron")
    print os.system('echo "*/1 * * * * %s/vanilla_sync.py > %s/vanilla_sync.log" >> /tmp/tmpcron' % (script_path, log_path))
    print os.system('echo "0 6 * * * %s/unconfirmed_shift_notifications.sh > %s/shift_notification.log" >> /tmp/tmpcron' % (script_path, log_path))
    print os.system('crontab /tmp/tmpcron')
    print os.system('rm /tmp/tmpcron')

import_redcross_db()
config_vanilla_sql()
import_vanilla_db()
config_vanilla()
mkcachedir()
config_scripts()
config_rkheaderplugin()
config_navigation()
setup_cron()
